package cn.lls.controller.sa;

import cn.lls.base.common.LlsEnum;
import cn.lls.base.exception.LlsException;
import cn.lls.base.result.R;
import cn.lls.base.result.ResultCode;
import cn.lls.entity.company.CompanyBaseEntity;
import cn.lls.req.company.AuditCompanyReq;
import cn.lls.req.company.ConditionSearchCompanyReq;
import cn.lls.service.ICompanyAllInfoService;
import cn.lls.service.ICompanyBaseService;
import cn.lls.vo.company.CompanyAllInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * 超级管理员对公司的操作，查看、审核等
 *
 * @ClassName AuditController
 * @Author 划水艺术家
 * @Date 2022/11/19 23:40
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/sa/ope")
@Api(tags = "超级管理员-公司操作控制器")
public class OpeController {

    @Autowired
    private ICompanyAllInfoService companyAllInfoService;

    @Autowired
    private ICompanyBaseService companyBaseService;


    /*
    超级管理员分页条件查询公司
     */
    @PostMapping("/info")
    @ApiOperation(value = "超级管理员分页条件查询公司", notes = "超级管理员分页条件查询公司", response = R.class, httpMethod = "POST")
    public R getCompanyInfoByPageAndCondition(
            @RequestBody @Validated
            ConditionSearchCompanyReq conditionSearchCompanyReq,
            BindingResult bindingResult){
        try {
            CompanyAllInfoVO companyAllInfoVO = new CompanyAllInfoVO();
            BeanUtils.copyProperties(companyAllInfoService.getCompanyInfoByPageAndCondition(conditionSearchCompanyReq),
                    companyAllInfoVO);
            return R.success(companyAllInfoVO);
        } catch (Exception e){
            return R.fail();
        }
    }

    /*
    超级管理员审核公司，拒绝/同意公司注册要求以及封禁/解封公司
     */
    @PutMapping("/audit")
    @ApiOperation(value = "超级管理员审核公司，拒绝/同意公司注册要求以及封禁/解封公司", notes = "超级管理员审核公司，拒绝/同意公司注册要求以及封禁/解封公司", response = R.class, httpMethod = "PUT")
    public R auditCompany(
                    @RequestBody @Validated
                    AuditCompanyReq auditCompanyReq,
                    BindingResult bindingResult){
        try {
            CompanyBaseEntity companyBase = companyBaseService.getById(auditCompanyReq.getCompanyId());
            if (companyBase == null)
                return R.fail(ResultCode.DATA_ERROR, "公司指定错误,不存在当前公司");
            // 判断行为是否异常
            if (!companyAllInfoService.verifyBehavior(auditCompanyReq.getBehavior(), companyBase.getCompanyStatus()))
                return R.fail(ResultCode.EXE_ERROR, "行为异常，这是不合理的操作");
            // 根据行为码做出相应操作
            companyAllInfoService.executeByBehavior(auditCompanyReq, companyBase);
            // 更改公司状态
            companyBaseService.changeCompanyStatus(auditCompanyReq.getCompanyId(), LlsEnum.behavior2Status(auditCompanyReq.getBehavior()));
            return R.success();
        }catch (LlsException e){
            e.printStackTrace();
            return R.fail(e.getCode(), e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }




}
